package br.sales.view.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.util.AgentUtils;
import org.primefaces.util.AjaxRequestBuilder;
import org.primefaces.util.HTML;
import org.primefaces.util.MessageFactory;

import com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer;

import br.sales.controller.INewsletterControler;
import br.sales.controller.NewsletterControlerImpl;
import br.sales.entity.Newsletter;
import br.sales.entity.exception.EntityExistsException;
import br.sales.entity.exception.EntityNotExistsException;
import br.sales.persistence.DB4OAppSingleton;
import br.sales.persistence.DB4ODAO;
import br.sales.persistence.exception.DBContainerNotInitializedException;
import br.sales.view.util.ViewUtil;


@ViewScoped
@ManagedBean(name="newsletterBean")
public class NewsletterBean implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private INewsletterControler newsletterControler;
	
	private Newsletter newsletter;
	private String email;
	private List<Newsletter> newsletters;
	private String keyword;
	
	public NewsletterBean() {
		newsletterControler = NewsletterControlerImpl.getInstance();
	}
	
	public void clear(boolean clearMainObject){
		if (clearMainObject){
			newsletter = null;
		}
		this.email = "";
		this.keyword = "";
	}
	
	public void search(){
		try {
			this.newsletters = newsletterControler.listLike(keyword);
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void prepareUpdate(){
		this.email = newsletter.getEmail();
	}
	
	public void prepareDelete(){
		clear(false);
	}
	
	public void cancelEditNewsletter(){
		clear(true);
	}
	
	public void prepareList(){
		try {
			newsletters = new ArrayList<Newsletter>();
			newsletters = newsletterControler.listAll();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			e.printStackTrace();
			newsletters = new ArrayList<Newsletter>();
		}
	}
	
	public void active(){
		try {
			newsletter = newsletterControler.retrieve(newsletter.getEmail());
			newsletter.setActive(true);
			newsletterControler.update(newsletter);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"newsletter_activated"), FacesMessage.SEVERITY_INFO);
			newsletter = null;
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			newsletter.setActive(false);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		}  catch (EntityExistsException e) {
			newsletter.setActive(false);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"email_in_use"), FacesMessage.SEVERITY_ERROR);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void deactivate(){
		try {
			newsletter = newsletterControler.retrieve(newsletter.getEmail());
			newsletter.setActive(false);
			newsletterControler.update(newsletter);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"newsletter_deactivated"), FacesMessage.SEVERITY_INFO);
			newsletter = null;
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			newsletter.setActive(true);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		}  catch (EntityExistsException e) {
			newsletter.setActive(true);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"email_in_use"), FacesMessage.SEVERITY_ERROR);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void update(){
		String oldemail = newsletter.getEmail();
		try {
			newsletter = newsletterControler.retrieve(oldemail);
			newsletter.setEmail(email);
			newsletterControler.update(newsletter);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"newsletter_updated"), FacesMessage.SEVERITY_INFO);
			clear(true);
			RequestContext context = RequestContext.getCurrentInstance();
			context.addCallbackParam(ViewUtil.CALLBACK_CLOSE, true);
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		}  catch (EntityExistsException e) {
			newsletter.setEmail(oldemail);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"email_in_use"), FacesMessage.SEVERITY_ERROR);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			newsletter.setEmail(oldemail);
		}

	}
	
	public void delete(){
		try {
			newsletterControler.delete(newsletter);
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"newsletter_has_been_deleted"), FacesMessage.SEVERITY_INFO);
			newsletter = null;
			prepareList();
		} catch (DBContainerNotInitializedException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"database_error"), FacesMessage.SEVERITY_FATAL);
			return;
		}  catch (EntityNotExistsException e) {
			ViewUtil.addMessage(FacesContext.getCurrentInstance(),ViewUtil.getResourceBundle(FacesContext.getCurrentInstance(),"invalid_newsletter"), FacesMessage.SEVERITY_ERROR);
			return;
		}
	}

	public Newsletter getNewsletter() {
		return newsletter;
	}

	public void setNewsletter(Newsletter newsletter) {
		this.newsletter = newsletter;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<Newsletter> getNewsletters() {
		return newsletters;
	}

	public void setNewsletters(List<Newsletter> newsletters) {
		this.newsletters = newsletters;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	
	
}
